/***************************************************************************
 创建者: 华磊
 开始时间: 2019.3.11
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************
 *  @file x.h
 *  功能:
 *
 *                                                                         *
 ***************************************************************************/

#ifndef MODBUSMANAGER_H
#define MODBUSMANAGER_H
#include <QMutex>
#include <QMap>
#include <QObject>
#include <QQueue>
#include <QString>
#include "fleetdefine.h"
#include <queue>

class QTimer;

class ModbusServer;
class ModbusMaster;
class PowerManager;
typedef struct _modbus modbus_t;

struct RobotModbusServerInfo
{
    int isValid;
    int robotId;
    int robotReadStartIndex;
    int robotReadEndIndex;
    int robotWriteStartIndex;
    int robotWriteEndIndex;
    int regSize;
    int port;
};

class ModbusManager:public QObject
{
   Q_OBJECT
public:
    ModbusManager(QString configFileIn, PowerManager *powerManageIn);
public:
    int getFleetCommandLog(std::queue<string> &fleetCommandLogOut);
    int createModbusTcpServer(int portIn);
    int createModbusTcpClient(QString ipAndPortIn);
    int deleteModbusTcpServer(int portIn);
    int deleteModbusTcpClient(QString ipAndPortIn);
    int readModbusRegister_Server(bool isTcp, int portIn, int regAddIn, int &regValueOut);
    int writeModbusRegister_Server(bool isTcp, int portIn, int regAddIn, int regValueIn);
    int readModbusRegister_TcpClient(QString ipAndPortIn,int regAddIn,int &regValueOut);
    int writeModbusRegister_TcpClient(QString ipAndPortIn,int regAddIn,int regValueIn);
    int readModbusRegister_rtuClient(int slaveIdIn,int regAddIn,int &regValueOut);
    int writeModbusRegister_rtuClient(int slaveIdIn,int regAddIn,int regValueIn);
private:
    int read_registers_tcp(modbus_t* tcpClientIn, int address, int &valueOut);
    int write_register_tcp( modbus_t* tcpClientIn,int address,int value);
    int getModbusTcpClient(QString ipAndPortIn,modbus_t* &tcpClientOut);
    int read_registers_rtu( int modbusId,int address,int &valueOut);
    int write_register_rtu(int modbusId,int address,int value);
    int checkModbusMaster(int modbusId);
    int connectPort(int modbusId);
    double transToDistance(int hight,int low);//返回单位米
    double transToAngle(int hight,int low);//返回单位degree
    int saveFleetCommand(QString commandInfo, int serialCodeIn);
    int processModbusFleetRequest(int modbusServerTypeIn, int modbusServerAdressIn);
    int processModbusFleetRequest_easyRegisterCommand(int modbusServerTypeIn, int modbusServerAdressIn);
    int sendBasicInfo(int modbusServerTypeIn, int modbusServerAdressIn);
    int exchangeRobotRegister();
    int initialModbusServer(int modbusServerTypeIn,QString modbusServerAdressIn);
    int initialRobotModbusServer();
    int initialConfigFile(QString configFileName);

    void addMsg(int messageLevel,
                 std::string componentName,
                 std::string messageType,
                 int messageCode,
                 int robotId,
                 int parameter1 = 0,
                 int parameter2 = 0,
                 int parameter3 = 0,
                 int parameter4 = 0 );

signals:
    void createModbusTcpServerSignal(int portIn,int regSizeIn);
private slots:
    void createModbusTcpServerSlot(int portIn,int regSizeIn);
    void createModbusComServerSlot(int comIdIn);
    void timeOut_slot();

private:
    UserInfo userInfo;
    unsigned char beatheartRegister;
    PowerManager *powerManage;
    bool isInitialOkStatus;
    QMutex usingMutex;
    QMap<int,ModbusServer*> modbusTcpServers;//本机port,支持多个server
    QMap<int,ModbusServer*> modbusComServers;//本机com id,支持多个server
//    QMap<QString,ModbusMaster*> modbusTcpClients;//ip:port
//    QMap<QString,modbus_t*> modbusTcpClients;//ip:port
//    QMap<int,modbus_t*> modbusRtuClients;//slaveId,暂时只支持１个串口
    int modbusTcpServerDafulatId;//控制器作为服务器的默认id
    int externTcpServerDafulatId;//外部ModbusTcp服务器的默认id
    bool startFinishedStatus;
    QTimer* timer;
    bool isUseSystemInfoMap;
    int modbusServerType;//ModbusConnection 0 serial 1 tcp
    QString modbusServerAdress;//TcpModbus就是端口号
    int address_batteryLeftLightModeRatio;
    int address_currentPointIndex;
    int address_currentPointMovingState;
    int address_heartBeat;
    int address_message_errorCode;
    int address_message_warnCode;
    int address_message_noticeCode;
    int address_basicRunState;
    int address_command;
    int fixed_address_setDo;

    int address_currentPointRemainDistance;
    int address_currentPointRemainAngle;
    int address_currentPos_x;
    int address_currentPos_y;
    int address_currentPos_r;
    int address_lineSpeed;
    int address_angleSpeed;
    int address_fleetStatus;
    int address_loadStatus;
    int address_executedCommandSerialId;
    int address_passedStationId;

    int doRegisterValidMask;

    double targetArriveDistanceError;
    double targetArriveAngleError;

    std::queue<std::string> fleetCommandLog;
    QMutex fleetLogMutex;
    int maxFleetCatchLineForDebug;
    int isUseModbusFleet;
    QMap<int,QString> mapList;
    QMap<int,QString> musicList;
    QMap<int,QString> programNameList;

    //串口master
    CommunicateParameter comParameter_master0;
    modbus_t* modbusRtuMaster0;
    int modbusRtuMaster0_currentSlaveId;
    QVector<RobotModbusServerInfo> robotModbusServerInfos;
    int timerPeriod;


};

#endif // MODBUSMANAGER_H
